package com.shengsiyuan.netty.fifthexample;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author 荀彧 2021/1/6
 */
public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        // 获取channel pipeline
        ChannelPipeline pipeline = ch.pipeline();
        // http的handler
        pipeline.addLast(new HttpServerCodec());
        // 以块的方式来写的处理器。
        pipeline.addLast(new ChunkedWriteHandler());
        // 是一个通道处理器，会对http消息进行聚合，
        pipeline.addLast(new HttpObjectAggregator(8192));
        // ws是路径的意思。 客户端向服务器发送消息的请求地址。
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

        pipeline.addLast(new TextWebSocketFrameHandler());
    }
}

